#include <stdio.h>
#include <list>
#include <string.h>
using namespace std;

#define TRACE(x...) 
#define PRINT(x...) TRACE(printf(x))

list<int> blocks[30];
char com1[50], com2[50];
int n;
int a, b;
list<int>::iterator it;
list<int>::iterator posA;
list<int>::iterator posB;
list<int> temp;
int listA, listB;
int f;

void find() {
	f=0;

	//for a
	for (int i=0; i<n; i++) {
		for (it=blocks[i].begin(); it!=blocks[i].end(); it++)
			if (*it==a) {
				f=1; listA=i; posA=it; break;
			}
		if (f) break;
	}

	f=0;

	//for b
	for (int i=0; i<n; i++) {
		for (it=blocks[i].begin(); it!=blocks[i].end(); it++)
			if (*it==b) {
				f=1; listB=i; posB=it; break;
			}
		if (f) break;
	}
}

int bak;

int main() {
	scanf("%d", &n);

	for (int i=0; i<n; i++) {
		blocks[i].push_back(i);
	}

	TRACE(
			for (int i=0; i<n; i++) {
			printf("%d: ", i);

			for (it=blocks[i].begin(); it!=blocks[i].end(); it++)
			printf("%d ", *it);
			printf("\n");
			}

			printf("\n");
	     )

		while (1) {
			scanf(" %s", com1);

			if (!strcmp(com1, "quit")) break;

			scanf("%d %s %d", &a, com2, &b);
			if (a==b) continue;

			find();
			if (listA==listB) continue;

			if (!strcmp(com1, "move")) {
				if (!strcmp(com2, "onto")) {
					while (blocks[listB].back()!=*posB) {
						bak=(blocks[listB].back());
						blocks[bak].push_back(bak);
						blocks[listB].pop_back();			
					}
				}

				while (blocks[listA].back()!=*posA) {
					bak=(blocks[listA].back());
					blocks[bak].push_back(bak);
					blocks[listA].pop_back();
				}

				blocks[listB].push_back(a);
				blocks[listA].pop_back();
			}
			else {
				temp.clear();

				if (!strcmp(com2, "onto")) {
					while (blocks[listB].back()!=*posB) {
						bak=(blocks[listB].back());
						blocks[bak].push_back(bak);
						blocks[listB].pop_back();			
					}
				}

				while (blocks[listA].back()!=*posA) {
					bak = blocks[listA].back();
					temp.push_back(bak);
					blocks[listA].pop_back();
				}

				blocks[listB].push_back(a);
				blocks[listA].pop_back();

				while (!temp.empty()) {
					bak = temp.back();
					temp.pop_back();
					blocks[listB].push_back(bak);
				}
			}

			TRACE(
					for (int i=0; i<n; i++) {
					printf("%d: ", i);

					for (it=blocks[i].begin(); it!=blocks[i].end(); it++)
					printf("%d ", *it);
					printf("\n");
					}

					printf("\n");
			     )

		}

	for (int i=0; i<n; i++) {
		printf("%d:", i);

		for (it=blocks[i].begin(); it!=blocks[i].end(); it++)
			printf(" %d", *it);
		printf("\n");
	}


	return 0;
}
